package com.kuaizu.es.config;

import jakarta.annotation.PostConstruct;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.json.JsonXContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * @author: wangmx15
 * @date: 2025/8/23
 **/
@Configuration
public class EsConFig {
    @Value("${elasticsearch.nodes[0].host}")
    private String host;
    @Value("${elasticsearch.nodes[0].port}")
    private Integer port;
    @Value("${elasticsearch.nodes[0].scheme}")
    private String scheme;
    /**
     * 定义RestHighLevelClient Bean，供Spring注入使用
     */
    @Bean
    public RestHighLevelClient restHighLevelClient() {
        // 构建客户端（根据实际ES服务器地址修改）
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost(host ,port, scheme)
                )
        );
        return client;
    }

    /**
     * 销毁客户端（Spring会自动调用@Bean的销毁方法）
     */
    @Bean(destroyMethod = "close")
    public RestHighLevelClient destroyClient() {
        return restHighLevelClient();
    }


}
